package com.ambulance.project.manager.service.impl;

import com.ambulance.common.constant.Constants;
import com.ambulance.common.utils.SecurityUtils;
import com.ambulance.project.manager.domain.BizHospital;
import com.ambulance.project.manager.mapper.BizHospitalMapper;
import com.ambulance.project.manager.service.IBizHospitalService;
import com.ambulance.project.manager.utils.MgtCommonUtil;
import com.ambulance.project.manager.utils.MgtUserLoginResult;
import com.ambulance.project.system.domain.SysUser;
import com.ambulance.project.system.service.ISysUserService;
import com.ambulance.utils.CommonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 外援Service业务层处理
 *
 * @author zr
 * @date 2020-05-12
 */
@Service
public class BizHospitalServiceImpl implements IBizHospitalService {
    @Autowired
    private BizHospitalMapper bizHospitalMapper;
    @Autowired
    private MgtCommonUtil mgtCommonUtil;
    @Autowired
    private ISysUserService iSysUserService;

    /**
     * 查询外援
     *
     * @param hospitalID 外援ID
     * @return 外援
     */
    @Override
    public BizHospital selectBizHospitalById(Long hospitalID) {
        BizHospital bizHospital = bizHospitalMapper.selectBizHospitalById(hospitalID);
        bizHospital.setPasswd(Constants.PASSWORD_MARK);
        return bizHospital;
    }

    /**
     * 查询外援列表
     *
     * @param bizHospital 外援
     * @return 外援
     */
    @Override
    public List<BizHospital> selectBizHospitalList(BizHospital bizHospital) {
        final MgtUserLoginResult mgtUserLoginResult = mgtCommonUtil.getMgtUserLoginResult();
        bizHospital.setUid(new Long(mgtUserLoginResult.getUid()));
//        bizHospital.setStatus(1L);
//        bizHospital.setType(1);
        return bizHospitalMapper.selectBizHospitalList(bizHospital);
    }

    public List<BizHospital> selectBizHospitalList2(BizHospital bizHospital) {
        final MgtUserLoginResult mgtUserLoginResult = mgtCommonUtil.getMgtUserLoginResult();
        bizHospital.setUid(new Long(mgtUserLoginResult.getUid()));
        bizHospital.setStatus(1L);
        return bizHospitalMapper.selectBizHospitalList(bizHospital);
    }

    public List<BizHospital> selectBizHospitalList3(BizHospital bizHospital) {
        return bizHospitalMapper.selectBizHospitalList(bizHospital);
    }

    /**
     * 新增外援
     *
     * @param bizHospital 外援
     * @return 结果
     */
    @Override
    public int insertBizHospital(BizHospital bizHospital) {
        final MgtUserLoginResult mgtUserLoginResult = mgtCommonUtil.getMgtUserLoginResult();
        bizHospital.setUid(new Long(mgtUserLoginResult.getUid()));
        // todo 根据type不同为实体类填充不同的数据
        if (bizHospital.getType().equals(2)) {
            // 当填充的数据类型为科室时 ,将本医院的名称获取到填充到表中
            final SysUser sysUser = iSysUserService.selectUserById(new Long(mgtUserLoginResult.getUid()));
            bizHospital.setHospitalName(sysUser.getNickName());
        }
        // 密码缺省123456
        if (CommonUtils.checkNull(bizHospital.getPasswd())) {
            bizHospital.setPasswd("123456");
        }
        bizHospital.setPasswd(SecurityUtils.encryptPassword(bizHospital.getPasswd()));
        return bizHospitalMapper.insertBizHospital(bizHospital);
    }

    /**
     * 修改外援
     *
     * @param bizHospital 外援
     * @return 结果
     */
    @Override
    public int updateBizHospital(BizHospital bizHospital) {
        String passwd = bizHospital.getPasswd();
        if (Constants.PASSWORD_MARK.equals(passwd)) {
            bizHospital.setPasswd(null);
        }
        return bizHospitalMapper.updateBizHospital(bizHospital);
    }

    /**
     * 批量删除外援
     *
     * @param hospitalIDs 需要删除的外援ID
     * @return 结果
     */
    @Override
    public int deleteBizHospitalByIds(Long[] hospitalIDs) {
        return bizHospitalMapper.deleteBizHospitalByIds(hospitalIDs);
    }

    /**
     * 删除外援信息
     *
     * @param hospitalID 外援ID
     * @return 结果
     */
    @Override
    public int deleteBizHospitalById(Long hospitalID) {
        return bizHospitalMapper.deleteBizHospitalById(hospitalID);
    }

    @Override
    public int frostBizHospitalById(Long hospitalID) {
        return bizHospitalMapper.frostBizHospitalById(hospitalID);
    }
}
